#import bge
#logic = bge.logic
#cont = logic.getCurrentController()
#own = cont.owner

#ARMATURE ANIMATION#
#######################################
import bge

def getArm():
    """ Searches setup for armature, do not use the name None """
    own = bge.logic.getCurrentController.owner
    # no skeleton
    skele = False
    # search children
    if(len(own.children) > 0):
        for i in own.children:
            if("channels" in dir(i)):
                own["skele"] = i
                skele = True
    # am I a skeleton?
    if(skele == False):
        if("channels" in dir(own)):
            own["skele"] = own
            skele = True
    # trying parent, last ditch
    if(skele == False):
        try:
            if("channels" in dir(own.parent)):
                own["skele"] = own.parent
                skele = True
        except:#< needed
            own["skele"] = "None"
    return skele
            
def setArm():
    own = bge.logic.getCurrentController.owner
    """ Sets the armature, you do not need a property, it sets one up """
    # set the skeleton as needed
    working = False
    if("skele" not in own.getPropertyNames()):
        working = getArm()
    else:
        if(own["skele"] == ""):
            working = getArm()
    if("skele" in own.getPropertyNames() and own["skele"] != "None"):
        working = True
            
    return working

def PlayAct(strAnim, intStart, intEnd, intLayer, intPri, fltSpeed):
    own = bge.logic.getCurrentController.owner
    """
    If you have a skele property, it must be empty
    needs animation name, start, end, layer, priority, and speed
    """
    # set skeleton, check if true
    working = setArm()
    # play anim if true
    if(working == True):
        # saving/loading may ruin the skeleton pointer, this should fix that in a tick
        try:        
            own["skele"].playAction(strAnim,intStart,intEnd, intLayer, intPri, .5, 2, .5, 0, fltSpeed)
        except:
            pass
        else:
            own["skele"] = ""
    # tell user they forgot the skeleton
    else:
        print("No armature in the setup")
	#playAction(name, start_frame, end_frame, layer=0, priority=0, blendin=0,
    # play_mode=ACT_MODE_PLAY, layer_weight=0.0, ipo_flags=0, speed=1.0)
    
    
#END ARMATURE ANIMATION#
#######################################